<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Adminis
  Date: 2025/3/19
  Time: 11:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分配角色</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/admin/css/bootstrap.min.css">
	<script src="${pageContext.request.contextPath}/admin/js/jquery-2.1.0.min.js"></script>
	<script src="${pageContext.request.contextPath}/admin/js/bootstrap.min.js"></script>
    <style>
		.custom-container{
			margin-top: 20px;
		}
       
    </style>
</head>
<body>

    <div class="container-fluid custom-container">
		<div class="form-group">
			<label class="checkbox-inline">
				<input type="checkbox" id="optAll" onclick="toggleAll()"> 全选
			</label>
		</div>
		<form action="" method="post">
			<div class="form-group" class="has-success">
				<c:forEach items="${acl_roles}" var="role">
					<label class="checkbox-inline">
						<input ${role.checked? 'checked' : ''} type="checkbox" class="opt" value="${role.role_id}" onchange="updateOptAll()">${role.role_name}
					</label>
				</c:forEach>
			</div>
			<button type="button" onclick="save()" class="btn btn-primary">保存</button>
		</form>
    </div>
    
</body>
</html>
<script>
	function toggleAll() {
		var optAll = document.getElementById('optAll');
		var checkboxes = document.getElementsByClassName('opt');
		for (var i = 0; i < checkboxes.length; i++) {
			checkboxes[i].checked = optAll.checked;
		}
	}

	function updateOptAll() {
		var optAll = document.getElementById('optAll');
		var checkboxes = document.getElementsByClassName('opt');
		var allChecked = true;
		for (var i = 0; i < checkboxes.length; i++) {
			if (!checkboxes[i].checked) {
				allChecked = false;
				break;
			}
		}
		optAll.checked = allChecked;
	}

	function save() {
		var checkboxes = document.getElementsByClassName('opt');
		var role_ids = '';
		for (var i = 0; i < checkboxes.length; i++) {
			if (checkboxes[i].checked) {
				role_ids += checkboxes[i].value + ',';
			}
		}
		role_ids = role_ids.substring(0, role_ids.length - 1);
		location.href = '${pageContext.request.contextPath}/acl_user/insertAcl_user_role?user_id=${user_id}&role_ids=' + role_ids;
}



</script>